<?php
App::uses('AppHelper', 'View/Helper');

class TabPanelHelper extends AppHelper {

	public $helpers = array('Html', 'Js' => 'Jquery');

	protected $_titles = array();
	protected $_content = array();

	public function tabStart($title) {
		$this->_titles[] = $title;
		$this->_View->start("tabPanelContent");
	}

	public function tabEnd() {
		$this->_View->end();
		$this->_content[] = $this->_View->fetch('tabPanelContent');
		$this->_View->assign('tabPanelContent', '');
	}

	public function get() {
		if(empty($this->_titles))
			return '';

		$keys = array_keys($this->_titles);
		$tabs = array_map(array($this, 'tabTitleLink'), $keys, $this->_titles);
		$divs = array_map(array($this, 'tabContent'),   $keys, $this->_content);

		$this->Js->buffer('$("#tabPanel").tabs();', true);
		return $this->Html->tag('div', "\n"
						. $this->Html->nestedList($tabs) . "\n"
						. implode("\n", $divs) . "\n"
						, array('id' => 'tabPanel')
						);
	}

	private function tabTitleLink($key, $value) {
		return $this->Html->link($value, '#tab-'.$key);
	}
	private function tabContent($key, $value) {
		return $this->Html->tag('div', $value, array('id' => 'tab-'.$key));
	}
}
